package zhongbo.java.air.dictionary.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.MyException;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirCraftMapper;
import zhongbo.java.air.dictionary.model.AirCraft;
import zhongbo.java.air.dictionary.service.IAirCraftService;

import java.util.Date;


@Service
public class AirCraftServiceImpl implements IAirCraftService {
    @Autowired
    private AirCraftMapper airCraftMapper;


    @Override
    public AirCraft addAir(AirCraft airCraft) throws MyException {
        Date dNow = new Date();
        airCraft.setCreateTime(dNow);
        airCraft.setUpdateTime(dNow);

//        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
        if (airCraft.getIcaoCode().matches("^\\w{6}$")) {
            if (airCraftMapper.selectByIcaoCode(airCraft.getIcaoCode()) == null) {
                if (airCraft.getRegistration().matches("^[A-Z]-\\d{1,4}$")) {
                        if (airCraft.getStatus().matches("^(ACTIVE|MAINTENANCE|RETIRED)$")) {
                            if (airCraftMapper.selectByRegistration(airCraft.getRegistration()) == null) {
                                int ok = airCraftMapper.insert(airCraft);
                                if (ok <= 0) {
                                    throw new MyException("增加失败");
                                }
                                return airCraftMapper.selectByIcaoCode(airCraft.getIcaoCode());
                            } else {
                                throw new MyException("注册号已存在");
                            }
                        } else {
                            throw new MyException("状态格式错误");
                        }

                } else {
                    throw new MyException("注册号格式错误");
                }

            } else {
                throw new MyException("ICAO 24位地址码已存在");
            }
        } else {
            throw new MyException("ICAO 24位地址码格式错误");
        }

    }

    @Override
    public String deleteAir(String id) throws MyException {
        int ok = airCraftMapper.deleteById(id);
        if (ok <= 0) {
            throw new MyException("删除失败");
        }
        return String.valueOf(ok);
    }

    @Override
    public AirCraft updateAir(AirCraft airCraft) throws MyException {
        Date dNow = new Date();
        airCraft.setUpdateTime(dNow);
        if (airCraft.getIcaoCode() == null) {
            throw new MyException("ICAO 24位地址码不能为空");
        }
        if (airCraft.getRegistration() != null) {
            throw new MyException("注册号不能修改");
        }
        if (airCraft.getStatus() != null) {
            if (!airCraft.getStatus().matches("^(ACTIVE|MAINTENANCE|RETIRED)$")) {
                throw new MyException("状态格式错误");
            }
        }
        if (airCraft.getIcaoCode().matches("^\\w{24}$")) {
            QueryWrapper<AirCraft> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("icao_code", airCraft.getIcaoCode());
            int ok = airCraftMapper.update(airCraft, queryWrapper);
            if (ok <= 0) {
                throw new MyException("更新失败");
            }
            return airCraftMapper.selectByIcaoCode(airCraft.getIcaoCode());
        } else {
            throw new MyException("ICAO 24位地址码格式错误");
        }

    }

    @Override
    public AirCraft selectAirById(String id) {
        return airCraftMapper.selectById(id);
    }

    @Override
    public PageVo<AirCraft> page(Integer pageNumber, Integer pageSize, boolean order, String searchField, String keyword, String sortField) {
        QueryWrapper<AirCraft> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(searchField)) {
            String[]fieldsArray = searchField.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        if (StrUtil.isNotBlank(sortField)) {
            if (order) {
                queryWrapper.orderByAsc(sortField);
            } else {
                queryWrapper.orderByDesc(sortField);
            }
        }

        IPage<AirCraft> pageParam = new Page<>(pageNumber, pageSize);
        IPage<AirCraft> resultPage = airCraftMapper.selectPage(pageParam, queryWrapper);

        return new PageVo<>(resultPage);
    }
    //registration,icaoCode,airline,aircraftType
}

